package com.derbysoft.ota.application.translator.rs;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.interfaces.protocol._2009a.AvailRequestSegmentsType.AvailRequestSegment;
import com.derbysoft.ota.interfaces.protocol._2009a.AvailRequestSegmentsType.AvailRequestSegment.RoomStayCandidates.RoomStayCandidate;
import com.derbysoft.ota.interfaces.protocol._2009a.HotelSearchCriteriaType.Criterion;
import com.derbysoft.ota.interfaces.protocol._2009a.HotelSearchCriterionType;
import com.derbysoft.ota.interfaces.protocol._2009a.HotelSearchCriterionType.RoomStayCandidates;
import com.derbysoft.ota.interfaces.protocol._2009a.ItemSearchCriterionType.HotelRef;
import com.derbysoft.ota.interfaces.protocol._2009a.OTAHotelAvailRS.Criteria;
import com.derbysoft.ota.interfaces.protocol._2009a.RoomStayCandidateType;
import com.googlecode.functionalcollections.FunctionalIterables;

public class HotelAvailRSCriteriaTranslator implements Translator<AvailRequestSegment, Criteria> {

	public Criteria translate(AvailRequestSegment source) {
		Criteria criteria = new Criteria();
		HotelSearchCriterionType searchCriterionType = new HotelSearchCriterionType();
		searchCriterionType.getHotelRef().add(hotelRef(source));
		searchCriterionType.setStayDateRange(source.getStayDateRange());
		searchCriterionType.setRoomStayCandidates(roomStayCandidates(source));
		criteria.getCriterion().add(searchCriterionType);
		return criteria;
	}

	private RoomStayCandidates roomStayCandidates(AvailRequestSegment source) {
		RoomStayCandidates target = new RoomStayCandidates();

		com.derbysoft.ota.interfaces.protocol._2009a.AvailRequestSegmentsType.
				AvailRequestSegment.RoomStayCandidates stayCandidates = source.getRoomStayCandidates();
		RoomStayCandidate sourceRoomStayCandidate = FunctionalIterables.make(stayCandidates.getRoomStayCandidate())
				.messagedIterator("Please provide at least 1 RoomStayCandidate.").next();

		RoomStayCandidateType roomStayCandidateType = new RoomStayCandidateType();
		roomStayCandidateType.setGuestCounts(sourceRoomStayCandidate.getGuestCounts());
		roomStayCandidateType.setQuantity(sourceRoomStayCandidate.getQuantity());
		target.getRoomStayCandidate().add(roomStayCandidateType);
		return target;
	}

	private HotelRef hotelRef(AvailRequestSegment source) {
		Criterion criterion = FunctionalIterables.make(source.getHotelSearchCriteria().getCriterion())
				.messagedIterator("Please provide at least 1 HotelSearchCriteriaType.Criterion.").next();
		return FunctionalIterables.make(criterion.getHotelRef())
				.messagedIterator("Please provide at least 1 HotelSearchCriteriaType.Criterion.HotelRef").next();
	}
}
